package com.yjc.hire.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.github.pagehelper.PageHelper;
import com.yjc.bo.DataDictionaryBO;
import com.yjc.hire.enums.YesOrNo;
import com.yjc.hire.exceptions.GraceException;
import com.yjc.hire.grace.result.PageGridResult;
import com.yjc.hire.grace.result.ResponseStatusEnum;
import com.yjc.hire.server.mapper.DataDictionaryMapper;
import com.yjc.hire.server.service.DataDictionaryService;
import com.yjc.pojo.DataDictionary;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/8/9
 * </p>
 */

@Service
public class DataDictionaryServiceImpl implements DataDictionaryService {

    @Autowired
    private DataDictionaryMapper dataDictionaryMapper;

    @Override
    public void createOrUpdateDataDict(DataDictionaryBO dataDictionaryBO) {
        DataDictionary dictionary = new DataDictionary();
        BeanUtils.copyProperties(dataDictionaryBO, dictionary);
        if (StringUtils.isBlank(dataDictionaryBO.getId())){
            //无id进行新增
            DataDictionary ddExist = dataDictionaryMapper
                    .selectDDByItemKeyAndItemValue(dataDictionaryBO.getItemKey(),
                            dataDictionaryBO.getItemValue());
            if (ddExist != null){
                GraceException.disPlay(ResponseStatusEnum.DATA_DICT_EXIST_ERROR);
            }
            dataDictionaryMapper.insert(dictionary);
        }else {
            dataDictionaryMapper.updateById(dictionary);
        }
    }

    @Override
    public PageGridResult<List<DataDictionary>> pageDataDictionary(String typeName, String itemValue, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<DataDictionary> ddList
        = dataDictionaryMapper.selectDataDictionaryList(typeName,itemValue);
        return PageGridResult.of(ddList,page);
    }

    @Override
    public DataDictionary getDataDictionaryById(String dictId) {
        return dataDictionaryMapper.selectById(dictId);
    }

    @Override
    public void deleteDataDictionaryById(String dictId) {
        dataDictionaryMapper.deleteById(dictId);
    }

    @Override
    public List<DataDictionary> getDataDictionaryByType(String typeCode) {
        List<DataDictionary> ddlist =
                dataDictionaryMapper.getDictionaryByTypeCode(typeCode, YesOrNo.YES.type);
        //用户端筛选已启用的
        return ddlist;
    }

    @Override
    public List<DataDictionary> getItemsByKeys(String... keys) {
        List<DataDictionary> list = dataDictionaryMapper.selectList(
                new QueryWrapper<DataDictionary>()
                        .eq("enable", YesOrNo.YES.type)
                        .in("item_key", keys)
        );
        return list;
    }
}
